package cn.iocoder.yudao.module.order.controller.admin.memberstream;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.order.controller.admin.memberstream.vo.*;
import cn.iocoder.yudao.module.order.dal.dataobject.memberstream.MemberStreamDO;
import cn.iocoder.yudao.module.order.convert.memberstream.MemberStreamConvert;
import cn.iocoder.yudao.module.order.service.memberstream.MemberStreamService;

@Tag(name = "管理后台 - 会员流水")
@RestController
@RequestMapping("/order/member-stream")
@Validated
public class MemberStreamController {

    @Resource
    private MemberStreamService memberStreamService;

    @PostMapping("/create")
    @Operation(summary = "创建会员流水")
    @PreAuthorize("@ss.hasPermission('order:member-stream:create')")
    public CommonResult<Integer> createMemberStream(@Valid @RequestBody MemberStreamCreateReqVO createReqVO) {
        return success(memberStreamService.createMemberStream(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员流水")
    @PreAuthorize("@ss.hasPermission('order:member-stream:update')")
    public CommonResult<Boolean> updateMemberStream(@Valid @RequestBody MemberStreamUpdateReqVO updateReqVO) {
        memberStreamService.updateMemberStream(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员流水")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('order:member-stream:delete')")
    public CommonResult<Boolean> deleteMemberStream(@RequestParam("id") Integer id) {
        memberStreamService.deleteMemberStream(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员流水")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('order:member-stream:query')")
    public CommonResult<MemberStreamRespVO> getMemberStream(@RequestParam("id") Integer id) {
        MemberStreamDO memberStream = memberStreamService.getMemberStream(id);
        return success(MemberStreamConvert.INSTANCE.convert(memberStream));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员流水列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('order:member-stream:query')")
    public CommonResult<List<MemberStreamRespVO>> getMemberStreamList(@RequestParam("ids") Collection<Integer> ids) {
        List<MemberStreamDO> list = memberStreamService.getMemberStreamList(ids);
        return success(MemberStreamConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员流水分页")
    @PreAuthorize("@ss.hasPermission('order:member-stream:query')")
    public CommonResult<PageResult<MemberStreamRespVO>> getMemberStreamPage(@Valid MemberStreamPageReqVO pageVO) {
        PageResult<MemberStreamDO> pageResult = memberStreamService.getMemberStreamPage(pageVO);
        return success(MemberStreamConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员流水 Excel")
    @PreAuthorize("@ss.hasPermission('order:member-stream:export')")
    @OperateLog(type = EXPORT)
    public void exportMemberStreamExcel(@Valid MemberStreamExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemberStreamDO> list = memberStreamService.getMemberStreamList(exportReqVO);
        // 导出 Excel
        List<MemberStreamExcelVO> datas = MemberStreamConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员流水.xls", "数据", MemberStreamExcelVO.class, datas);
    }

}
